sink("C:/Users/Hyunjung/Desktop/ISQ_FINAL_Manuscrips and Data/Datafiles/datafiles/datalog.txt")
require(dplyr)
library(ggplot2)
library(rio)
library(survival)

# Import data
data1 <- import("C:/Users/Hyunjung/Desktop/ISQ_FINAL_Manuscrips and Data/Datafiles/datafiles/datafile.csv")

# Table 1: Descriptive statistics
library(psych)
describeBy(data1)
#take n, mean, sd, min, max

# Table 2: The Effects of Rebel Governance Types on Peace Duration
## Model 1 
coxpart<-coxph(Surv(peacedur, recur) ~ participate+nonparticipate, cluster = conflict_id, data=data1)
summary(coxpart)
## Model 2
coxpart_cont3<-coxph(Surv(peacedur, recur) ~ participate+nonparticipate+gov.vic+reb.vic+low.act+polity2+gdppc_ln+rebstrength+secession, cluster = conflict_id, data=data1) 
summary(coxpart_cont3)
## Model 3
coxpart_cont3.1<-coxph(Surv(peacedur, recur) ~ participate+nonparticipate+gov.vic+reb.vic+low.act+polity2+gdppc_ln+rebstrength+secession+finance2+extsup_NSA+terrcont, cluster = conflict_id, data=data1) 
summary(coxpart_cont3.1)

library(stargazer)
stargazer(coxpart,coxpart_cont3,coxpart_cont3.1, align=TRUE,  type="html", dep.var.labels="Peace Duration",
          title="Table 2: The Effects of Rebel Governance Types on Peace Duration",
          covariate.labels=c("Participatory Governance","Non-Participatory Governance","Government Victory","Rebel Victory","Low Activities","Regime Type","GDP/cap(ln)","Rebel Srength","Secessionist", "Natural Resources","External Support","Territory control"),
          no.space=TRUE, keep.stat = "n", out="C:/Users/Hyunjung/Desktop/ISQ_FINAL_Manuscrips and Data/Datafiles/T2.cox_main.doc")



# Figure 1: Rebel Governance Types and War Recurrence
cor <- data1 
cor$recur <- factor(cor$recur, levels=c(0,1), labels=c("Peace", "War Recurrence"))
cor$governance <- factor(cor$governance, levels=c(1, 0), labels=c("Rebel Governance", "No Rebel Governance"))
cor$participate <- factor(cor$participate, levels=c(1,0), labels=c("Participatory Governance", "Non-Participatory Governance"))
cor$polpart <- factor(cor$polpart, levels=c(1,0), labels=c("Rebel Political Participation", "No Rebel Political Participation"))

cor_sub <- cor %>% filter(governance=="Rebel Governance")

tiff(file="C:/Users/Hyunjung/Desktop/ISQ_FINAL_Manuscrips and Data/Datafiles/F1.RebGovandWarrecur.tiff", width=1900, height=2000, res=300)
barplot(table(cor_sub$recur, cor_sub$participate), 
           col = c("#56B4E9","steelblue"),
           xlab = "Rebel Governance Type",
           ylim=range(0,160),
)

text(0.7, 72, "66", cex=1.6)
text(1.9, 132, "127", cex=1.6)
text(0.7, 23, "46(70%)", cex=1.5, col = "white")
text(0.7, 53, "20(30%)", cex=1.5, col = "white")
text(1.9, 30, "66(52%)", cex=1.5, col = "white")
text(1.9, 100, "61(48%)", cex=1.5, col = "white")

legend("topleft",
       cex=1.6, c("Recurrnence", "Peace"),
       fill = c("steelblue","#56B4E9")
)
dev.off()


# Figure 2: Hazard Ratio for Rebel Governance Type 
dataa <- data1
colnames(x = dataa)[colnames(x = dataa)== "participate"] <- 'Participatory Governance'
colnames(x = dataa)[colnames(x = dataa)== "nonparticipate"] <- 'Non-Participatory Governance'
colnames(x = dataa)[colnames(x = dataa)== "neg.set"] <- 'Negotiated Settlement'
colnames(x = dataa)[colnames(x = dataa)== "gov.vic"] <- 'Government Victory'
colnames(x = dataa)[colnames(x = dataa)== "reb.vic"] <- 'Rebel Victory'
colnames(x = dataa)[colnames(x = dataa)== "low.act"] <- 'Low Activity'
colnames(x = dataa)[colnames(x = dataa)== "polity2"] <- 'Regime Type'
colnames(x = dataa)[colnames(x = dataa)== "gdppc_ln"] <- 'GDP/cap(ln)'
colnames(x = dataa)[colnames(x = dataa)== "rebstrength"] <- 'Rebel Strength'
colnames(x = dataa)[colnames(x = dataa)== "secession"] <- 'Secessionist'


coxpart_cont_plot<-coxph(Surv(peacedur, recur) ~ `Participatory Governance`+`Non-Participatory Governance`+`Government Victory`+`Rebel Victory`+`Low Activity`+`Regime Type`+`GDP/cap(ln)`+`Rebel Strength`+ `Secessionist`, cluster = conflict_id, data=dataa)

library(survminer)
tiff(file="C:/Users/Hyunjung/Desktop/ISQ_FINAL_Manuscrips and Data/Datafiles/F2.tiff", width=1900, height=2000, res=300)
ggforest(coxpart_cont_plot, data = dataa)
dev.off()

# Figure 3: Survival Curves by Governance Type
data_sub <- data1 %>% filter(governance==1)
survfit_part <- survfit(Surv(peacedur, recur) ~ participate, data = data_sub) 

library(survminer)
tiff(file="C:/Users/Hyunjung/Desktop/ISQ_FINAL_Manuscrips and Data/Datafiles/F3.SurvCurvs.tiff", width=1900, height=2000, res=300)
ggsurvplot(
  survfit_part,
  data = data1,
  main = "Survival curves",
  size = .5,                 # change line size
  conf.int = TRUE,          # Add confidence interval
  pval = FALSE,              # Add p-value
  risk.table = FALSE,        # Add risk table
  risk.table.col = "strata",# Risk table color by groups
  legend ="bottom",
  legend.title = "Governance Type",
  legend.labs =
    c("Non-Participatory Governance", "Participatory Govrnance"),    # Change legend labels
  ylim = c(0.35, 1),
  ggtheme = theme_bw()      # Change ggplot2 theme
)
dev.off()

# Appendix A Determinants of Rebel Governance
## Table A1: Determinants of Rebel Governance
glm_gov2<-glm(governance ~ extsup_NSA+finance2+secession+rebstrength+terrcont+polity2+gdppc_ln+v2x_partip, data = data1,  family = binomial("probit"))
glm_part2<-glm(participate ~ extsup_NSA+finance2+secession+rebstrength+terrcont+polity2+gdppc_ln+v2x_partip, data = data1,  family = binomial("probit"))
summary(glm_gov2)
summary(glm_part2)
stargazer(glm_gov2, glm_part2, align=TRUE,  dep.var.labels=c("Rebel Governance","Participatory Governance"), type="html", 
          title = "Table A1: Determinants of Rebel Governance",
          covariate.labels=c("External Support","Natural Resource", "Secessionist","Rebel Strength","Territory control","Regime Type","GDP/cap(ln)","Pre-Conflict Civilian Participation"),
          no.space=TRUE, keep.stat = "n",out="C:/Users/Hyunjung/Desktop/ISQ_FINAL_Manuscrips and Data/Datafiles/TA1.firststage.doc")



# Appendix B Robustness Checks
## Table B1: Propensity Score Matching Analysis
library(MatchIt)
m_ps <- glm(participate ~ polity2+gdppc_ln+rebstrength+secession+gov.vic+reb.vic+low.act, family = binomial(), data = data1)
prs_df <- data.frame(pr_score = predict(m_ps, type = "response"), participate = m_ps$model$participate)
data1_nomiss <- data1 %>% dplyr::select(participate,polity2,gdppc_ln,rebstrength,secession, peacedur, recur, nonparticipate, gov.vic,reb.vic,low.act) %>%
  na.omit()
mod_match <- matchit(participate ~ polity2+gdppc_ln+rebstrength+secession+gov.vic+reb.vic+low.act, method = "nearest", data = data1_nomiss)
dta_m <- match.data(mod_match) # create a dataframe containing only the matched observations

coxpart_cont9_me2<-coxph(Surv(peacedur, recur) ~ participate+nonparticipate+gov.vic+reb.vic+low.act+polity2+gdppc_ln+rebstrength+secession, data=dta_m, robust = TRUE)
summary(coxpart_cont9_me2)
stargazer(coxpart_cont9_me2, align=TRUE,  dep.var.labels="Peace Duration", type="html", 
          title = "Table B1: Propensity Score Matching Analysis",
          covariate.labels=c("Participatory Governance","Non-Participatory Governance","Government Victory","Rebel Victory","Low Activities","Regime Type","GDP/cap(ln)","Rebel Srength","Secessionist"),
          no.space=TRUE, keep.stat = "n",out="C:/Users/Hyunjung/Desktop/ISQ_FINAL_Manuscrips and Data/Datafiles/TB1.matching.doc")

## Table B2: Estimation of Duration Model with Governance Provision Time
data1 <- data1 %>% mutate(partgovnot5y=ifelse(participate==1&partgov5y==0,1,0))
coxpartgovyear<-coxph(Surv(peacedur, recur) ~ partgov5y+partgovnot5y+nonparticipate+gov.vic+reb.vic+low.act+polity2+gdppc_ln+rebstrength+secession, cluster = conflict_id, data=data1)
summary(coxpartgovyear)
stargazer(coxpartgovyear, align=TRUE,  dep.var.labels="Peace Duration", type="html",
          title ="Table B2: Estimation of Duration Model with Governance Provision Time",
          covariate.labels=c("Participatory Governance (Provided last 5 years)","Participatory Governance (Did not provided last 5 years)","Non-Participatory Governance","Government Victory","Rebel Victory","Low Activities","Regime Type","GDP/cap(ln)","Rebel Strength","Secessionist"),
          no.space=TRUE, keep.stat = "n",out="C:/Users/Hyunjung/Desktop/ISQ_FINAL_Manuscrips and Data/Datafiles/TB2.last5year.doc")

## Table B3: Controlling for Pre-conflict Civilian Participation
coxpart_cont2<-coxph(Surv(peacedur, recur) ~ participate+nonparticipate+gov.vic+reb.vic+low.act+polity2+gdppc_ln+rebstrength+secession+v2x_partip, cluster = conflict_id, data=data1) # include pre-conflict participation
coxpart_cont2.1<-coxph(Surv(peacedur, recur) ~ participate+nonparticipate+gov.vic+reb.vic+low.act+polity2+gdppc_ln+rebstrength+secession+preelection, cluster = conflict_id, data=data1) # include pre-conflict participation
summary(coxpart_cont2)
summary(coxpart_cont2.1)
stargazer(coxpart_cont2,coxpart_cont2.1, align=TRUE,  dep.var.labels="Peace Duration", type="html",
          title = "Table B3: Controlling for Pre-conflict Civilian Participation",
          covariate.labels=c("Participatory Governance","Non-Participatory Governance","Government Victory","Rebel Victory","Low Activities","Regime Type","GDP/cap(ln)","Rebel Srength","Secessionist", "Pre-conflict Civilian Participation(V-dem)", "Pre-conflict Civilian Participation(Election)"),
          no.space=TRUE, keep.stat = "n",out="C:/Users/Hyunjung/Desktop/ISQ_FINAL_Manuscrips and Data/Datafiles/TB3.civilpart.doc")

## Table B4: Controlling for Conflict duration and intensity
coxpart_cont6<-coxph(Surv(peacedur, recur) ~ participate+nonparticipate+gov.vic+reb.vic+low.act+polity2+gdppc_ln+rebstrength+secession+confdur_ln, cluster = conflict_id, data=data1) # include conflict duration
coxpart_cont6.1<-coxph(Surv(peacedur, recur) ~ participate+nonparticipate+gov.vic+reb.vic+low.act+polity2+gdppc_ln+rebstrength+secession+intensity, cluster = conflict_id, data=data1) # include conflict severity

stargazer(coxpart_cont6, coxpart_cont6.1, align=TRUE, type="html", dep.var.labels="Peace Duration",
          title = "Table B4: Controlling for Conflict duration and intensity",
          covariate.labels=c("Participatory Governance","Non-Participatory Governance","War Outcome: Government Victory","War Outcome: Rebel Victory","War Outcome: Low Activities","Regime Type","GDP/cap(ln)","Rebel Srength","Secessionist", "Conflict Duration(ln)", "Conflict Intensity"),
          no.space=TRUE, keep.stat = "n",out="C:/Users/Hyunjung/Desktop/ISQ_FINAL_Manuscrips and Data/Datafiles/TB4.cox_conintendur.doc")

## Table B5: Controlling for UN Peace Keeping Operations
coxpart_cont4.3<-coxph(Surv(peacedur, recur) ~ participate+nonparticipate+gov.vic+reb.vic+low.act+polity2+gdppc_ln+rebstrength+secession+unpk, cluster = conflict_id, data=data1) # include pko
stargazer(coxpart_cont4.3, align=TRUE,  dep.var.labels="Peace Duration",type="html",
          title ="Table B5: Controlling for UN Peace Keeping Operations",
          covariate.labels=c("Participatory Governance","Non-Participatory Governance","Regime Type","GDP/cap(ln)","Rebel Srength","Secessionist", "UN Peace Keeping Operations"),
          no.space=TRUE, keep.stat = "n",out="C:/Users/Hyunjung/Desktop/ISQ_FINAL_Manuscrips and Data/Datafiles/TB5.cox_pko.doc")


## Table B6: Controlling for Rebel Political Participation
coxpart_cont4<-coxph(Surv(peacedur, recur) ~ participate+nonparticipate+gov.vic+reb.vic+low.act+polity2+gdppc_ln+rebstrength+secession+polpart_five, cluster = conflict_id, data=subset(data1,election_five_mgep==1)) # include rebel political participation
stargazer(coxpart_cont4, align=TRUE,  type="html", dep.var.labels="Peace Duration",
          title = "Table B6: Controlling for Rebel Political Participation",
          covariate.labels=c("Participatory Governance","Non-Participatory Governance","Government Victory","Rebel Victory","Low Activities","Regime Type","GDP/cap(ln)","Rebel Srength","Secessionist", "Rebel Political Participation"),
          no.space=TRUE, keep.stat = "n", out="C:/Users/Hyunjung/Desktop/ISQ_FINAL_Manuscrips and Data/Datafiles/TB6.cox_polpart.doc")

## Table B7: Controlling for Power-sharing Provisions; Interactive Effect of Participatory Governance and Power-sharing Provisions
data1 <- data1 %>% mutate(pa_polpowsharing2 = ifelse(is.na(pa_polpowsharing) & neg.set == 1, 0, pa_polpowsharing)) 
cox_ct7<-coxph(Surv(peacedur, recur) ~ participate+nonparticipate+polity2+gdppc_ln+rebstrength+secession+pa_polpowsharing2, cluster = conflict_id, data=subset(data1, neg.set==1)) # include PA : political power-sharing
cox_ct7_in<-coxph(Surv(peacedur, recur) ~ participate+nonparticipate+polity2+gdppc_ln+rebstrength+secession+pa_polpowsharing2+participate:pa_polpowsharing2, cluster = conflict_id, data=subset(data1, neg.set==1)) # include PA : political power-sharing


stargazer(cox_ct7, cox_ct7_in, align=TRUE,  dep.var.labels="Peace Duration", type="html",
          title="Table B7: Controlling for Power-sharing Provisions; Interactive Effect of Participatory Governance and Power-sharing Provisions",
          order = c("participate","nonparticipate","participate:pa_polpowsharing2", "polity2","gdppc_ln","rebstrength","secession","pa_polpowsharing2"),
          covariate.labels=c("Participatory Governance","Non-Participatory Governance","ParticipatoryXPowerSharing", "Regime Type","GDP/cap(ln)","Rebel Srength","Secessionist", "Power-Sharing Provisions"),
          no.space=TRUE, keep.stat = "n",out="C:/Users/Hyunjung/Desktop/ISQ_FINAL_Manuscrips and Data/Datafiles/TB6.papowsharing.doc")


## Tabke B8: Interactive Effect of Participatory Governance and Rebel Victory
coxpart_cont5<-coxph(Surv(peacedur, recur) ~ participate*reb.vic+nonparticipate+gov.vic+low.act+polity2+gdppc_ln+rebstrength+secession, cluster = conflict_id, data=data1) # interaction 
summary(coxpart_cont5)
stargazer(coxpart_cont5, align=TRUE,  dep.var.labels="Peace Duration", type="html",
          title="Tabke B8: Interactive Effect of Participatory Governance and Rebel Victory",
          covariate.labels=c("Participatory Governance","Rebel Victory","Non-Participatory Governance","Government Victory","Low Activities","Regime Type","GDP/cap(ln)","Rebel Srength","Secessionist", "Participattory Governance*Rebel Victory"),
          no.space=TRUE, keep.stat = "n",out="C:/Users/Hyunjung/Desktop/ISQ_FINAL_Manuscrips and Data/Datafiles/TB8.rebvicinter.doc")

# Appendix C Additional Models
## Table C1: Effects of Participatory Governance (A model without non-participatory governance)
coxpart_onlypart<-coxph(Surv(peacedur, recur) ~ participate+gov.vic+reb.vic+low.act+polity2+gdppc_ln+rebstrength+secession, cluster = conflict_id, data=data1) # NEW main: mil victory -> reb.vic + gov.vic
stargazer(coxpart_onlypart, align=TRUE,  type="html", dep.var.labels="Peace Duration",
          title="Table C1: Effects of Participatory Governance (A model without non-participatory governance)",
          covariate.labels=c("Participatory Governance","Government Victory","Rebel Victory","Low Activities","Regime Type","GDP/cap(ln)","Rebel Srength","Secessionist"),
          no.space=TRUE, keep.stat = "n", out="C:/Users/Hyunjung/Desktop/ISQ_FINAL_Manuscrips and Data/Datafiles/TC1.cox_onlypart.doc")


## Table C2: The Interactive Effect of Participatory Governance (last 5 years) and Negotiated Settlement
coxpartgovyear_inter<-coxph(Surv(peacedur, recur) ~ partgov5y+partgovnot5y+nonparticipate+neg.set+gov.vic+reb.vic+polity2+gdppc_ln+rebstrength+secession+partgov5y:neg.set, cluster = conflict_id, data=data1)
stargazer(coxpartgovyear_inter, align=TRUE,  dep.var.labels="Peace Duration", type="html", 
          title="Table C2: The Interactive Effect of Participatory Governance (last 5 years) and Negotiated Settlement",
          covariate.labels=c("Participatory Governance (Provided last 5 years)","Participatory Governance (Did not provided last 5 years)","Non-Participatory Governance","Negotiated settlement","Government Victory","Rebel Victory","Regime Type","GDP/cap(ln)","Rebel Srength","Secessionist", "Participatory Governance(provided last 5 years)*Negotiated Settlement"),
          no.space=TRUE, keep.stat = "n",out="C:/Users/Hyunjung/Desktop/ISQ_FINAL_Manuscrips and Data/Datafiles/TC2.last5yinter.doc")


## Table C3: Estimation with the Weibull Model
coxpart_cont1.1 <-survreg(Surv(peacedur, recur) ~ participate+nonparticipate+gov.vic+reb.vic+low.act+polity2+gdppc_ln+rebstrength+secession, data=data1, dist = "weibull")

stargazer(coxpart_cont1.1, align=TRUE,  dep.var.labels="Peace Duration", type="html", 
          title="Table C3: Estimation with the Weibull Model",
          covariate.labels=c("Participatory Governance","Non-Participatory Governance","Government Victory","Rebel Victory","Low Activity","Regime Type","GDP/cap(ln)","Rebel Strength", "Secessionist"),
          no.space=TRUE, keep.stat = "n", out="C:/Users/Hyunjung/Desktop/ISQ_FINAL_Manuscrips and Data/Datafiles/TC3.weibull.doc")

## Table C4: Estimation of Duration Models with Mixed Effects
library(coxme)
coxpart_me<-coxme(Surv(peacedur, recur) ~ participate+nonparticipate+gov.vic+reb.vic+low.act+polity2+gdppc_ln+rebstrength+secession+(1|side_b_id), data=data1) 

library(jstable)
x2<-jstable:::coxmeTable(coxpart_me) 
x2 <- x2 %>% mutate(mystar=ifelse(p < .01, "***", ifelse(p < .05, "**", ifelse(p < .1, "*", " ")))) %>%
  mutate(estimate=paste(round(beta,3),mystar,sep="")) %>% mutate(se=paste("(",round(se,3),")", sep="")) %>%
  mutate(estimate=paste(estimate, se, sep = "")) 
x <- x2 %>% dplyr::select(estimate)
rownames(x)[rownames(x) == "participate"] <- "Participatory Governance"
rownames(x)[rownames(x) == "nonparticipate"] <- "Non-Participatory Governance"
rownames(x)[rownames(x) == "gov.vic"] <- "Government Victory"
rownames(x)[rownames(x) == "reb.vic"] <- "Rebel Victory"
rownames(x)[rownames(x) == "low.act"] <- "Low Activity"
rownames(x)[rownames(x) == "polity2"] <- "Regime Type"
rownames(x)[rownames(x) == "gdppc_ln"] <- "GDP/cap(ln)"
rownames(x)[rownames(x) == "rebstrength"] <- "Rebel Strength"
rownames(x)[rownames(x) == "secession"] <- "Secessionist"
write.table(x, file="C:/Users/Hyunjung/Desktop/ISQ_FINAL_Manuscrips and Data/Datafiles/TC4.coxme.doc", sep = "\t", quote = FALSE)


## Table C5: The Effects of Rebel Governance Type on War Termination
glm_reb.vic_cont<-glm(reb.vic ~ participate + nonparticipate + finance2+secession+rebstrength+polity2+gdppc_ln+pko, data = data1,  family = binomial("probit"))
glm_neg.set_cont<-glm(neg.set ~ participate + nonparticipate + finance2+secession+rebstrength+polity2+gdppc_ln+pko, data = data1,  family = binomial("probit"))

stargazer(glm_reb.vic_cont, glm_neg.set_cont, align=TRUE,  dep.var.labels=c("Rebel Victory","Negotiated Settlement"), type="html",
          title="Table C5: The Effects of Rebel Governance Type on War Termination",
          covariate.labels=c("Participatory Governance","Non-Participatory Governance", "Natural Resources","Secessionist Groups","Rebel Strength","Regime Type","GDP/cap(ln)","PKO"),
          no.space=TRUE, keep.stat = "n",out="C:/Users/Hyunjung/Desktop/ISQ_FINAL_Manuscrips and Data/Datafiles/TC5.warter.doc")


# Appendix D Sensitivity of Coding
## Table D1: Diferent Participatory Institutions (Councils and Elections)
data1 <- data1 %>% mutate(noncouncilgov2=ifelse(governance2==1&council==0,1,0))
data1 <- data1 %>% mutate(elect2=ifelse(!is.na(elect),elect, participate1)) 
data1 <- data1 %>% mutate(election=ifelse(participate1==1|elect2==1, 1, 0)) %>% mutate(council=ifelse(council>0,1,council))
data1 <- data1 %>% mutate(nonelectgov=ifelse(governance1==1&election==0,1,0))

cox_council<-coxph(Surv(peacedur, recur) ~ council+noncouncilgov2+gov.vic+reb.vic+low.act+polity2+gdppc_ln+rebstrength+secession, cluster = conflict_id, data=data1) 
cox_election<-coxph(Surv(peacedur, recur) ~ election+nonelectgov+gov.vic+reb.vic+low.act+polity2+gdppc_ln+rebstrength+secession, cluster = conflict_id, data=data1) 
summary(cox_council)
summary(cox_election)
stargazer(cox_council, cox_election, align=TRUE,  dep.var.labels="Peace Duration", type="html",
          title="Table D1: Diferent Participatory Institutions (Councils and Elections)",
          covariate.labels=c("Council","Non-Council Governance","Election","Non-election Governance","Government Victory","Rebel Victory","Low Activities","Regime Type","GDP/cap(ln)","Rebel Srength","Secessionist"),
          no.space=TRUE, keep.stat = "n",out="C:/Users/Hyunjung/Desktop/ISQ_FINAL_Manuscrips and Data/Datafiles/TD1.codingsensitivity.doc")



